﻿
//using SpeechLib;
using System;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

public class SpeakMgr: XObjBase
{
    public static SpeakMgr Instance;
    public List<string> englishList;
    public List<string> chineseList;
    public string GetEnglish(ref int index)
    {
        return englishList[index];
    }
    public int curIndex = 0;
    
    public int maxIndex
    {
        get
        {
            return englishList.Count;
        }
    }
    protected override void Awake()
    {
        base.Awake();
        curIndex = PlayerPrefs.GetInt("SpeakMgr-Index:", 0);
        Instance = this;
    }
    //private SpVoice _v;

    private Action _callBack;
    public void Init(Action callBack)
    {
       // _v = new SpVoice();
       // _v.Voice = _v.GetVoices(string.Empty, string.Empty).Item(0);
        _callBack = callBack;
        Load();
    }

    public void Load()
    {
        this.LoadAssetAsync<TextAsset>(XRes.N("Dict", XResType.TextAsset), (textAsset) => {

            MemoryStream ms = new MemoryStream(textAsset.bytes);
            englishList = new List<string>();
            chineseList = new List<string>();
            StreamReader sr = new StreamReader(ms);
            string line;
            while ((line = sr.ReadLine()) != null)
            {
                var strs = line.Split('@');
                englishList.Add(strs[0]);
                chineseList.Add(strs[1]);
            }

            _callBack?.Invoke();
        });
    }

    public void Speak(string str)
    {
        //_v.Speak(str, SpeechVoiceSpeakFlags.SVSFDefault);
    }

    private void OnApplicationQuit()
    {
        Debug.Log("OnApplicationQuit");
        PlayerPrefs.SetInt("SpeakMgr-Index:", curIndex);
    }
}

